from flask import Blueprint,request
from loguru import logger
from app.utils.responseFunc import returnMsg


errorbp = Blueprint("error",__name__)

@errorbp.app_errorhandler(400)
def bad_request(e):
    logger.error(f"请求地址:{request.url}")
    logger.error(f"异常原因:{e}")
    return returnMsg(code=0,msg="请求参数的格式不正确,请检查你的请求参数再次请求"),400

@errorbp.app_errorhandler(404)
def page_not_found(e):
    logger.error(f"请求地址:{request.url}")
    logger.error(f"异常原因:{e}")
    return returnMsg(code=0,msg="请求地址不正确,请检查你的请求地址再次请求"),404

@errorbp.app_errorhandler(405)
def method_not_allowed(e):
    logger.error(f"请求地址:{request.url}")
    logger.error(f"异常原因:{e}")
    return returnMsg(code=0,msg="请求类型不正确,请检查你的请求类型再次请求"),405

@errorbp.app_errorhandler(Exception)
def server_error(e):
    logger.error(f"请求地址:{request.url}")
    logger.error(f"请求类型:{request.method}")
    logger.error(f"请 求 头:{request.headers}")
    logger.error(f"请求数据:{request.values.to_dict() or str(request.data,'utf-8')}")
    logger.error(f"异常原因:{e}")
    return returnMsg(code=0,msg="服务器异常"),500
